<?php
namespace App\Http\Controllers\Admin;

use App\Models\SiteMenu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Models\Permission;
/**
 *
 * @author Joker
 *
 */
class SiteMenuController extends BaseController
{

    /**
     * 菜单列表
     * @return \Illuminate\View\$this
     */
    public function index()
    {
        if(!$this->user->can('viewSettingMenu')) abort(405);
        
        $siteMenu = SiteMenu::where(array('status'=>0,'parent_id'=>0))->orderBy('menu_id','ASC')->paginate(20);
        return view('admin.setting.sitemenu.index')->with('siteMenu',$siteMenu);
    }

    /**
     * 菜单新增
     */
    public function create()
    {
        if(!$this->user->can('editSettingMenu')) abort(405);
        
        $perms = Permission::where('name','like','view%')->orWhere('pid','=',0)->get();
        $sitemenu = SiteMenu::where('parent_id','=',0)->get();
        return view('admin.setting.sitemenu.add')->with('sitemenu',$sitemenu)->with('perms',$perms)->with('blademode','add');
    }
    
    /**
     * 保存
     * @param Request $req
     */
    public function store(Request $req)
    {
        if(!$this->user->can('editSettingMenu')) abort(405);
        
        $data = $req->only('name','type','child_1','child_2','child_3','url','description','sort','status','icon','permission','menu_id','blademode');
        if(is_numeric(intval(trim($data['menu_id']))) && $data['menu_id'] > 0)
        {
            $sitemenu = SiteMenu::find($data['menu_id']);
        }
        else
        {
            $sitemenu = new SiteMenu();
        }
        
        $sitemenu->name         = $data['name'];
        $sitemenu->parent_id    = $this->getTypes($data['type'],$data['child_1'],$data['child_2'],$data['child_3']);
        $sitemenu->url          = $data['url'];
        $sitemenu->description  = $data['description'];
        $sitemenu->status       = $data['status'];
        $data['icon'] ?$sitemenu->icon         = $data['icon']:'';
        $data['permission'] ? $sitemenu->permission_id   = $data['permission']:'';
        $sitemenu->save();
        
        return redirect("admin/setting/menu");
            
    }
    
    public function edit($id)
    {
        if(!$this->user->can('viewSettingMenu')) abort(405);
        
        $siteMenu = SiteMenu::find($id);
        $pid = $siteMenu->parent_id;
        $upid = 0;
        $secondMenu = collect();
        if(count($siteMenu->parentMenu)>0)
        {
            $upid = $siteMenu->parentMenu->menu_id;
            $pid = $siteMenu->parentMenu->parent_id;
            $secondMenu = SiteMenu::getSiteMenu($pid);
        }
        $sitemenus = SiteMenu::where('parent_id','=',0)->get();
        $perms = Permission::where('name','like','view%')->orWhere('pid','=',0)->get();
        return view('admin.setting.sitemenu.add')->with('result',$siteMenu)->with('pid',$pid)->with('sitemenu',$sitemenus)
                ->with('upid',$upid)->with('perms',$perms)->with('blademode','edit')->with('secondmenu',$secondMenu);
    }
    
    /**
     * 获取子菜单列表
     * @param unknown $param
     */
    public function childSiteMenus(Request $req) 
    {
        $parent_id = $req->get('parentid',0);
        if($parent_id)
        {
            $result = SiteMenu::getSiteMenu($parent_id);
            if(empty($result))
            {
                return Response::json(array('status'=>'flase','msg'=>'没有更多分类','data'=>''));
            }
            else
            {
                return Response::json(array('status'=>'success','msg'=>'获取分类成功','data'=>$result));
            }
        }
        return Response::json(array('status'=>'false','msg'=>'没有更多分类','data'=>''));
    }
}